/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.parser.mp.main;

import cn.ac.iie.di.parser.mp.handler.ParserFileHandler;
import cn.ac.iie.di.parser.mp.mpost.MPostHandler;
import cn.ac.iie.di.parser.mp.utils.ConfigureUtil;
import cn.ac.iie.di.parser.mp.utils.LoadTableMapData;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * 该程序用于读取指定目录下文件，获取json对象，进行解析后处理，发送消息队列
 *
 * @author Administrator
 */
public class ParserMpMain {

    static Logger logger = null;
    public static String nameserver;
    public static String mpost_topic = "";
    public static String mpost_dir = "";
    public static int bufSize = 0;
    public static String localpath = "";
    public static int localFileSize = 0;
    public static int coreThreadPoolSize = 0;
    public static int maxThreadPoolSize = 0;
    public static int blockingQueueSize = 0;
    public static String tablename = "";
    public static int jsonThreadSize = 0;
    public static int intervaltime = 0;
    public static int timeout = 0;
    public static String[] serverPath = new String[]{};
    public static int requestServerSize = 0;
    public static ArrayBlockingQueue<String> mpostQueue = null;
    public static int isTianYa = 1;
    public static String yunrunURL = "";

    static {
        PropertyConfigurator.configure(ClassLoader.getSystemClassLoader().getResource("log4j.properties"));
        logger = Logger.getLogger(ParserMpMain.class.getName());
    }

    public static void main(String[] args) {
        try {
            ConfigureUtil conf = new ConfigureUtil();
            logger.info("init conf ParserMpMain");
            nameserver = conf.getProperty("nameserver");
            logger.info("the nameserver is " + nameserver);
            mpost_topic = conf.getProperty("mpost_topic");
            logger.info("the mpost-RocketMQ:  the topic is " + mpost_topic);
            //各个通道文件夹
            mpost_dir = conf.getProperty("mpost_dir");
            logger.info("mpost_dir is " + mpost_dir);
            bufSize = Integer.parseInt(conf.getProperty("bufferSize"));
            //文件路径参数
            localpath = conf.getProperty("localPath");
            localFileSize = Integer.parseInt(conf.getProperty("localFileSize"));
            //线程池参数
            coreThreadPoolSize = Integer.parseInt(conf.getProperty("coreThreadPoolSize"));
            maxThreadPoolSize = Integer.parseInt(conf.getProperty("maxThreadPoolSize"));
            blockingQueueSize = Integer.parseInt(conf.getProperty("blockingQueueSize"));

            tablename = conf.getProperty("tablename");
            //去重服务
            String serverPathStr = conf.getProperty("serverPath");
            serverPath = serverPathStr.split(";");
            requestServerSize = Integer.parseInt(conf.getProperty("requestServerSize"));

            //消费参数
            jsonThreadSize = Integer.parseInt(conf.getProperty("jsonThreadSize"));
            intervaltime = Integer.parseInt(conf.getProperty("intervaltime"));
            timeout = Integer.parseInt(conf.getProperty("timeout"));
            logger.info("the bufSize is " + bufSize + " , localpath is " + localpath + " , localFileSize is " + localFileSize
                    + " , coreThreadPoolSize is " + coreThreadPoolSize + " , maxThreadPoolSize is " + maxThreadPoolSize + " , blockingQueueSize is " + blockingQueueSize
                    + " , jsonThreadSize is " + jsonThreadSize + " , intervaltime is " + intervaltime + " , timeout is " + timeout);
            //初始化表信息
            new LoadTableMapData().init(tablename);
            logger.info("load table finish.");
            mpostQueue = new ArrayBlockingQueue(bufSize);
            yunrunURL = conf.getProperty("yunrunURL");
            logger.info("the yunrun URL is " + yunrunURL);
            //启动发送mq线程
            handle();
            //启动解析文件线程，与发送mq线程通过mpostQueue队列进行生产和消费
            parserFile();
        } catch (IOException ex) {
            logger.fatal(ex.getMessage(), ex);
            System.exit(-1);
        }
    }

    public static void parserFile() {
        String mpostPath = "";

        if (localpath.endsWith("/")) {
            mpostPath = localpath + mpost_dir;
        } else {
            mpostPath = localpath + "/" + mpost_dir;
        }

        //美篇消息
        Thread mpostThread = new Thread(new ParserFileHandler(mpostPath, mpostQueue));
        mpostThread.setName("###Parser_mpost");
        mpostThread.start();

    }

    public static void handle() {
        //美篇消息
        MPostHandler mpostHandler = new MPostHandler();
        Thread mpostThread = new Thread(mpostHandler);
        mpostThread.setName("##MPOST##");
        mpostThread.start();

    }

}
